package com.nightsoul.easypoi.test;

import org.apache.poi.ss.usermodel.Workbook;
import org.junit.Test;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.nightsoul.easypoi.test.entity.MsgClient;
import com.nightsoul.easypoi.test.entity.MsgClientGroup;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import cn.afterturn.easypoi.handler.impl.ExcelDataHandlerDefaultImpl;

/**
 * 大数据量导出 Created by JueYue on 2017/9/7.
 */
public class ExcelExportBigData {

    @Test
    public void bigDataExport() throws Exception {

        List<MsgClient> list = new ArrayList<MsgClient>();
        Workbook workbook = null;
        Date start = new Date();
        ExportParams params = new ExportParams("大数据测试", "测试");
        for (int i = 0; i < 200000; i++) {
            MsgClient client = new MsgClient();
            client.setBirthday(new Date());
            client.setClientName("小明" + i);
            client.setClientPhone("18797" + i);
            client.setCreateBy("JueYue");
            client.setId("1" + i);
            client.setRemark("测试" + i);
            MsgClientGroup group = new MsgClientGroup();
            group.setGroupName("测试" + i);
            client.setGroup(group);
            list.add(client);
            if(list.size() == 10000){
                workbook = ExcelExportUtil.exportBigExcel(params, MsgClient.class, list);
                list.clear();
            }
        }
        ExcelExportUtil.closeExportBigExcel();
        System.out.println(new Date().getTime() - start.getTime());
        File savefile = new File("D:/excel/");
        if (!savefile.exists()) {
            savefile.mkdirs();
        }
        FileOutputStream fos = new FileOutputStream("D:/excel/ExcelExportBigData.bigDataExport.xlsx");
        workbook.write(fos);
        fos.close();
    }

    @Test
    public void bigDataExport2() throws Exception {
    	List<Map<String, Object>> list = Lists.newArrayList();
    	Workbook workbook = null;
    	Date start = new Date();
    	ExportParams params = new ExportParams("大数据测试", "测试");
    	params.setDataHanlder(new ExcelDataHandlerDefaultImpl<Map<String, Object>>() {
			@Override
			public Object exportHandler(Map<String, Object> obj, String name, Object value) {
				Integer sex = (Integer) value;
				return sex == 0 ? "女" : "男";
			}
			
			@Override
			public String[] getNeedHandlerFields() {
				return new String[]{"性别"};
			}
		});
    	
    	List<ExcelExportEntity> studentColumns = Lists.newArrayList();
		studentColumns.add(new ExcelExportEntity("姓名", "name"));
		studentColumns.add(new ExcelExportEntity("性别", "sex"));
		
		ExcelExportEntity birthdayExcelExportEntiry = new ExcelExportEntity("生日", "birthday");
		birthdayExcelExportEntiry.setFormat("yyyy-MM-dd");
		studentColumns.add(birthdayExcelExportEntiry);
		ExcelExportEntity registrationDateExcelExportEntiry = new ExcelExportEntity("进校日期", "registrationDate");
		registrationDateExcelExportEntiry.setFormat("yyyy-MM-dd");
		studentColumns.add(registrationDateExcelExportEntiry);
    	
    	for (int i = 0; i < 2000000; i++) {
    		Map<String, Object> studentMap = Maps.newHashMap();
    		studentMap.put("name", "小明" + i);
    		studentMap.put("sex", i%2);
    		studentMap.put("birthday", new Date());
    		studentMap.put("registrationDate", new Date());
    		list.add(studentMap);
    		if(list.size() == 10000){
    			CombaExcelBatchExportServer batachServer = CombaExcelBatchExportServer.getExcelBatchExportServer(params, studentColumns);
    			workbook = batachServer.appendData(list);
    			list.clear();
    		}
    	}
    	//ExcelExportUtil.closeExportBigExcel();
    	System.out.println(new Date().getTime() - start.getTime());
    	File savefile = new File("D:/excel/");
    	if (!savefile.exists()) {
    		savefile.mkdirs();
    	}
    	FileOutputStream fos = new FileOutputStream("D:/excel/ExcelExportBigData.bigDataExport2.xlsx");
    	workbook.write(fos);
    	fos.close();
    }

}
